package com.one.square.gateway.handler;

import com.one.square.core.domain.Result;
import com.one.square.core.exception.GateWayException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author cwm
 * @Description
 * @date 2021/10/29 下午4:01
 * @Version 1.0
 */
@Slf4j
@Component
public class GateWayExceptionHandlerAdvice {
    @ExceptionHandler(value = {GateWayException.class})
    public Result handle(GateWayException ex) {
        return Result.failed(ex.getMessage());
    }

    @ExceptionHandler(value = {Throwable.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result handle(Throwable throwable) {

        if(throwable instanceof GateWayException) {
            return handle((GateWayException) throwable);
        }else {
            return Result.failed(throwable.getMessage());
        }
    }
}
